import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterlib/common/net/retrofit/post_api.dart';
import 'package:flutterlib/common/utils/distance_utils.dart';
import 'package:flutterlib/model/activity/activity_detail_bean.dart';
import 'package:flutterlib/model/post/post_detail_bean.dart';
import 'package:flutterlib/pages/activity/activity_join_member_page.dart';
import 'package:thrio/thrio.dart';

/**
    thrio通用跳转复用逻辑
 */
class NavigatorUtils {
  ///公共打开方式
  static NavigatorRouter(BuildContext context, Widget widget) {
    // return Navigator.push(
    //     context,
    //     new CupertinoPageRoute(
    //         builder: (context) => pageContainer(widget, context)));

    return Navigator.push<dynamic>(
      context,
      MaterialPageRoute<dynamic>(
        builder: (BuildContext context) => widget,
      ),
    );
  }

  static ThrioNavigatorPop<TParams>({
    TParams params,
    bool animated = true,
  }) {
    ThrioNavigator.pop(params: params);
  }

  ///Page页面的容器，做一次通用自定义
  static Widget pageContainer(widget, BuildContext context) {
    return MediaQuery(

        ///不受系统字体缩放影响
        data: MediaQuery.of(context).copyWith(textScaleFactor: 1),
        child: widget);
  }

  static Future goUserHome(BuildContext context, int userId) {
    return ThrioNavigator.push(url: '/user/main', params: {'userId': userId});
  }

  static Future goPostDetail(BuildContext context, int postId,
      {String name: ""}) {
    return ThrioNavigator.push(
        url: '/post/detail', params: {'postId': postId, 'title': name});
  }

  static Future goPostCard(
    BuildContext context,
  ) {
    return ThrioNavigator.push(url: '/post/card', params: {});
  }

  static Future goPostCreate(BuildContext context, int interestId, {int postId}) {
    return ThrioNavigator.push(
        url: '/post/create', params: {'interestId': interestId, 'postId': postId});
  }

  static Future goCardCreate(BuildContext context, int interestId) {
    return ThrioNavigator.push(
        url: '/post/createcard', params: {'interestId': interestId});
  }

  static Future gotoPostSearch(BuildContext context, String word) {
    return ThrioNavigator.push(url: '/post/search', params: {'content': word});
  }

  static Future goLogin(BuildContext context) {
    return ThrioNavigator.push(url: '/native/LoginAccountActivity');
  }

  static Future gotoInterestDetail(BuildContext context, int id) {
    return ThrioNavigator.push(
        url: '/interest/detail', params: {'interestId': id});
  }

  static Future gotoActivityDetail(BuildContext context, int id) {
    return ThrioNavigator.push(url: '/activity/detail', params: {'id': id});
  }

  static Future gotoActivityCreate(
      BuildContext context, int circleId, int interestId) {
    return ThrioNavigator.push(
        url: '/activity/create',
        params: {'circleId': circleId, 'interestId': interestId});
  }

  static Future gotoDeployActivityCreate(BuildContext context, int interestId) {
    return ThrioNavigator.push(
        url: '/activity/deployCreate', params: {'interestId': interestId});
  }

  static Future gotoActivityMine(BuildContext context) {
    return ThrioNavigator.push(url: '/activity/mine');
  }

  ///弹出 dialog
  static Future<T> showQuhuiDialog<T>({
    @required BuildContext context,
    bool barrierDismissible = true,
    WidgetBuilder builder,
  }) {
    return showDialog<T>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) {
          return MediaQuery(

              ///不受系统字体缩放影响
              data: MediaQueryData.fromWindow(WidgetsBinding.instance.window)
                  .copyWith(textScaleFactor: 1),
              child: new SafeArea(child: builder(context)));
        });
  }

  static Future<T> showQuhuiSheet<T>({
    @required BuildContext context,
    bool barrierDismissible = true,
    WidgetBuilder builder,
  }) {
    return showModalBottomSheet<T>(
        context: context,
        builder: (context) {
          return MediaQuery(

              ///不受系统字体缩放影响
              data: MediaQueryData.fromWindow(WidgetsBinding.instance.window)
                  .copyWith(textScaleFactor: 1),
              child: new SafeArea(child: builder(context)));
        });
  }

  static Future gotoActionSelectPage(BuildContext context) {
    return ThrioNavigator.push(url: '/home/select');
  }

  static Future gotoSelectAddress(
    BuildContext context,
    NavigatorParamsCallback poppedResult,
  ) {
    return ThrioNavigator.push(
        url: '/home/selectAddress', poppedResult: poppedResult);
  }

  static Future gotoSelectCity(BuildContext context) {
    return ThrioNavigator.push(url: '/home/selectCity');
  }

  static Future gotoJoinMemberPage(
      BuildContext context, List<Join_members> joinMembers) {
    return NavigatorRouter(
        context,
        ActivityJoinMemberPage(
          joinMembers: joinMembers,
        ));
  }

  static Future gotoActivityComment(int id) {
    return ThrioNavigator.push(
        url: '/activity/commentList', params: {'activityId': id});
  }

  static Future gotoShowAddress(BuildContext context, String address, Poi poi) {
    return ThrioNavigator.push(url: '/home/showAddress', params: {
      'address': address,
      'lon': poi.longitude,
      'lat': poi.latitude
    });
  }

  static Future gotoMyCircle(BuildContext context) {
    ThrioNavigator.push(url: '/native/ImCarrierActivity');
  }


  static Future gotoPersonTalk(BuildContext context, String userId) {
    ThrioNavigator.push(url: '/native/UUChatActivity', params: {
      'UserId': userId
    });
  }

  static void goSharePage(BuildContext context, PostDetailBean post) {

    ThrioNavigator.push(url: '/native/ShareActivity', params: {
      'title': post.title,
      'time' : post.createTime.toString(),
      'image' : post.thumbnail,
      'content': post.originalContent,
      'id': post.id.toString()
    });
  }

  static void gotoAddInterestPage(BuildContext context) {
    ThrioNavigator.push(url: '/interest/addInterest', params: {
    });
  }
}
